package cn.edu.sdjzu.xg.eagle.controller;

import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.factory.ServiceFactory;
import cn.edu.sdjzu.xg.eagle.service.SchoolServiceImpl;
import cn.edu.sdjzu.xg.eagle.service.SchoolServiceInterface;
import cn.edu.sdjzu.xg.eagle.util.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;



@WebServlet("/schoolCreate.ctl")
public class SchoolCreateCtl extends HttpServlet {
    private SchoolServiceInterface schoolServiceInterface = ServiceFactory.getService(SchoolServiceInterface.class,"SchoolServiceImol");



    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        String school_json = JSONUtil.getJSON(request);
        School schoolToAdd = JSON.parseObject(school_json, School.class);
        response.setContentType("application/json;charset=utf-8");
        JSONObject message = new JSONObject();
        try {
            schoolServiceInterface.add(schoolToAdd);
            message.put("msg","增加成功");
            message.put("code","200");

        } catch (SQLException e) {
            message.put("msg","数据库操作异常");
            message.put("code","200");
        } catch (Exception e) {
            message.put("msg","网络异常");
            message.put("code","200");
        }
        response.getWriter().write(JSON.toJSONString(message));
    }
}
